package com.zhenyi.jiahua.task;

import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;

import com.jfinal.kit.LogKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jiahua.common.model.JhArrange;
import com.jiahua.common.model.JhArrangeStudent;
import com.jiahua.common.model.JhCourse;
import com.jiahua.common.model.JhStudent;
import com.jiahua.common.model.JhStudentCourse;
import com.zhenyi.jiahua.core.utils.MessageUtils;

/**
 * 上课的回调
 * 
 * @author cen
 *
 */
public class JHOnCourseTask implements Runnable {

	// private void setAllToOnClass() {
	// SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindOnAllClassArranges",
	// new Date());
	// List<JhArrange> list = JhArrange.dao.find(sqlPara);
	// for (JhArrange jhArrange : list) {
	// jhArrange.setIsOnClass(1);
	// jhArrange.update();
	// }
	// }

	/**
	 * 选课时间之后的课 如果没人选,隐藏
	 */
	private void updateEmptyJoinCourse() {
		final Integer JOIN_TIME = PropKit.getInt("JOIN_TIME", 3600);
		Date date = new DateTime().plusSeconds(JOIN_TIME).toDate();
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindOnClassArranges", date);
		List<JhArrange> list = JhArrange.dao.find(sqlPara);
		// int size = list.size();
		// if (size > 0) {
		// LogKit.info("hide course " + list.size());
		// }
		for (JhArrange arrange : list) {
			if (arrange.getIsHide().equals(1)) {
				continue;
			}
			if (arrange.getJoinCount().equals(0)) {
				LogKit.info("hide " + arrange.getTopic());
				arrange.setIsHide(1);
				arrange.update();
			}
		}
	}

	@Override
	public void run() {
		updateEmptyJoinCourse();
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindOnClassArranges", new Date());
		List<JhArrange> list = JhArrange.dao.find(sqlPara);
		int size = list.size();
		if (size > 0) {
			LogKit.info("trigger on course " + list.size());
		}
		for (JhArrange arrange : list) {
			arrange.setIsOnClass(1);
			arrange.update();
			Long courseId = arrange.getCourseId();
			JhCourse course = JhCourse.dao.findById(arrange.getCourseId());
			SqlPara studentPara = Db.use("jiahua").getSqlPara("JHFindCourseJoinedStudents", arrange.getId());
			List<JhArrangeStudent> arrangeStudents = JhArrangeStudent.dao.find(studentPara);
			for (JhArrangeStudent arrangeStudent : arrangeStudents) {
				if (arrangeStudent.getStatus().equals(JhArrangeStudent.STATUS_CANCEL)) {
					continue;
				}
				// arrangeStudent.setStatus(JhArrangeStudent.STATUS_DONE);
				arrangeStudent.setIsDone(1);
				arrangeStudent.update();
				Long studentId = arrangeStudent.getStudentId();
				JhStudent student = JhStudent.dao.findById(studentId);
				Integer count = student.getCoreCourseLeft();
				try {
					String topic = arrange.getTopic();
					JhStudentCourse studentCourse = JhStudentCourse.dao.findById(courseId, studentId, topic);
					if (studentCourse == null) {
						studentCourse = new JhStudentCourse();
						studentCourse.setCourseName(course.getName());
						studentCourse.setCourseId(courseId);
						studentCourse.setTopic(topic);
						studentCourse.setStudentId(studentId);
						studentCourse.setCourseTime(arrange.getStartTime());
						studentCourse.setCount(1);
						studentCourse.save();
					} else {
						studentCourse.setCount(studentCourse.getCount() + 1);
						studentCourse.update();
					}
					student.updateOnCourseCount();
					student.update();
				} catch (Exception e) {
					LogKit.warn(studentId + " - " + arrange.getId() + " - " + courseId);
					LogKit.warn("student on course warn", e);
					sendErrorNotification(student);
				}
				if (!course.getIsCount().equals(1)) {
					continue;
				}
				if (count == null) {
					continue;
				}
				if (count > 0) {
					student.setCoreCourseLeft(student.getCoreCourseLeft() - 1);
					student.update();
				} else {
					sendErrorNotification(student);
				}
				if (count < 30) {
					sendNotification(student);
				}
			}
		}
	}

	private void sendNotification(JhStudent student) {
		String sendTitle = "学员" + student.getName() + "剩余课时数不足30,请留言";
		String sendContent = "";
		MessageUtils.send(student.getCcId(), null, sendTitle, sendContent);
		MessageUtils.send(student.getTaId(), null, sendTitle, sendContent);

	}

	private void sendErrorNotification(JhStudent student) {

	}

}
